#!/bin/bash
read -p "Please input rootpath name: " Rootname
read -p "Please input project name: " Projectname
read -p "Please input project type(lib/exe)[default:exe]: " Projecttype
read -p "Need zlib(y/n): " Needzlib
read -p "Need log4cxx(y/n): " Needlog4cxx
read -p "Need tinyxml(y/n): " Needtinyxml
mkdir -p $Rootname/include
mkdir -p $Rootname/src
mkdir -p $Rootname/test
mkdir -p $Rootname/bin
mkdir -p $Rootname/config
mkdir -p $Rootname/doc
mkdir -p $Rootname/debug
mkdir -p $Rootname/release
mkdir -p $Rootname/src/$Projectname
tar jvxf ./project.tar.bz2
mv -f ./cmake $Rootname
mv -f ./tlpr $Rootname/include
mv -f ./config.h.in $Rootname/include
#mv ./CMakeLists.txt ./CMakeLists.txt.tmp
sed -i "s#Server2#$Projectname#g" CMakeLists.txt
sed -i "s!FIND_PACKAGE(Kfs)!#&!g" CMakeLists.txt
sed -i "s!FIND_PACKAGE(Log4cpp)!#&!g" CMakeLists.txt
sed -i "s!^IF(NOT Log4cpp_FOUND)!#&!" CMakeLists.txt
sed -i 's!MESSAGE(STATUS "Can not find log4cpp which is need by kfsclient")!#&!g' CMakeLists.txt
sed -i 's|^ENDIF(NOT Log4cpp_FOUND)|#&|g' CMakeLists.txt
sed -i "s!^IF(NOT Kfs_FOUND)!#&!g" CMakeLists.txt
sed -i 's!Message(STATUS "Can not find kfs library in your system")!#&!g' CMakeLists.txt
sed -i 's!^ENDIF(NOT Kfs_FOUND)!#&!g' CMakeLists.txt
sed -i '/^#/d' CMakeLists.txt
sed -i "s!%%%%!$Projectname!g" CMakeLists.txt.2
if [ "$Needzlib" == "n" ]; then
    sed -i "s!FIND_PACKAGE(ZLIB)!#&!g" CMakeLists.txt
fi
if [ "$Needlog4cxx" == "n" ]; then
    sed -i "s!FIND_PACKAGE(Log4cxx)!#&!g" CMakeLists.txt
    sed -i "s!log4cxx!!g" CMakeLists.txt.2
fi
if [ "$Needtinyxml" == "n" ]; then
    sed -i "s!FIND_PACKAGE(TinyXML REQUIRED)!#&!g" CMakeLists.txt
    sed -i "s!^IF(TL_TINYXML_FOUND)!#&!g" CMakeLists.txt
    sed -i 's!MESSAGE(STATUS "Found tinyxml success")!#&!g' CMakeLists.txt
    sed -i "s!ELSE(TL_TINYXML_FOUND)!#&!g" CMakeLists.txt
    sed -i 's!MESSAGE(STATUS "Found tinyxml failed")!#&!g' CMakeLists.txt
    sed -i 's!^ENDIF(TL_TINYXML_FOUND)!#&!g' CMakeLists.txt
    sed -i 's!MESSAGE(STATUS ${TL_TINYXML_INCLUDE_DIR})!#&!g' CMakeLists.txt
    sed -i 's!tinyxml!!g' CMakeLists.txt.2
fi

if [ "$Projecttype" == "lib" ]; then
    sed -i "s!ADD_EXECUTABLE!ADD_LIBRARY!g" CMakeLists.txt.2
fi
#rm -rf ./CMakeLists.txt.tmp
mv -f ./CMakeLists.txt $Rootname
mv -f ./maketags.sh $Rootname
sed -i "s!%%%%!$Projectname!g" CMakeLists.txt.main
mv -f ./CMakeLists.txt.main $Rootname/src/CMakeLists.txt
mv -f ./CMakeLists.txt.2 $Rootname/src/$Projectname/CMakeLists.txt
mv -f ./main.cpp $Rootname/src/$Projectname
mv -f ./stdafx.h $Rootname/include
